#pragma once
//#include "mkvcontext.hpp"
#include "mkvstreamvideo.hpp"
#include "mpegparser/M2VParser.h"
#include <vector>
//#include <list>

namespace MkvMux
{

class MkvStreamVideoMpeg2 : public MkvStreamVideo
{
    MkvStreamVideoMpeg2(const MkvStreamVideoMpeg2&);
    MkvStreamVideoMpeg2& operator=(const MkvStreamVideoMpeg2&);
    
public:

    MkvStreamVideoMpeg2(MkvContext&, const AM_MEDIA_TYPE&);
    
    HRESULT Receive(IMediaSample*);
    int EndOfStream();
    void Flush();

protected:

    void WriteTrackName();
    void WriteTrackCodecID();
    void WriteTrackCodecPrivate();
    void WriteTrackCodecName();
    void WriteTrackSettings();

private:
    
    //typedef std::vector<BYTE> bytes_t;
    //bytes_t m_hdr;

    M2VParser m_parser;    
    
    void CreateNewFrame(MPEGFrame*);

    enum Type { kTypeIFrame, kTypePFrame, kTypeBFrame };

    class Mpeg2VideoFrame : public VideoFrame
    {
        Mpeg2VideoFrame(const Mpeg2VideoFrame&);
        Mpeg2VideoFrame& operator=(const Mpeg2VideoFrame&);
      
    private:
        MPEGFrame* const m_pFrame;

    public:
        explicit Mpeg2VideoFrame(MPEGFrame*);
        ~Mpeg2VideoFrame(); 

        void Write(const MkvStream&, ULONG) const;

        Type type() const;
        bool IsKey() const;

        ULONG size() const;
        const BYTE* data() const;

        ULONG curr_timecode_ms() const;

        SHORT prev_timecode_ms() const;
        //relative to curr_timecode_ms
        //must be less than 0

        SHORT next_timecode_ms() const;
        //relative to curr_timecode_ms
        //must be greater than 0

    };
    
public:   
    LONG last_timecode_ms() const;
    
protected:
    void GetSequenceHeader(const BYTE*&, ULONG&) const;

};

}  //end namespace MkvMux
